const numList1 = [{
    label: 7,
    value: 7
  },
  {
    label: 7.5,
    value: 7.5
  },
  {
    label: 8,
    value: 8
  },
  {
    label: 8.5,
    value: 8.5
  },
  {
    label: 9,
    value: 9
  },
  {
    label: 9.5,
    value: 9.5
  },
  {
    label: 10,
    value: 10
  },
  {
    label: 10.5,
    value: 10.5
  },
  {
    label: 11,
    value: 11
  },
  {
    label: 11.5,
    value: 11.5
  },
  {
    label: 12,
    value: 12
  },
  {
    label: 12.5,
    value: 12.5
  },
  {
    label: 13,
    value: 13
  },
];
const numList2 = [{
    label: 4,
    value: 4
  },
  {
    label: 4.5,
    value: 4.5
  },
  {
    label: 5,
    value: 5
  },
  {
    label: 5.5,
    value: 5.5
  },
  {
    label: 6,
    value: 6
  },
  {
    label: 6.5,
    value: 6.5
  },
  {
    label: 7,
    value: 7
  },
  {
    label: 7.5,
    value: 7.5
  },
  {
    label: 8,
    value: 8
  },
  {
    label: 8.5,
    value: 8.5
  },
  {
    label: 9,
    value: 9
  },
  {
    label: 9.5,
    value: 9.5
  },
  {
    label: 10,
    value: 10
  },
];
const numList3 = [{
    label: 0,
    value: 0
  },
  {
    label: 0.5,
    value: 0.5
  },
  {
    label: 1,
    value: 1
  },
  {
    label: 1.5,
    value: 1.5
  },
  {
    label: 2,
    value: 2
  },
  {
    label: 2.5,
    value: 2.5
  },
  {
    label: 3,
    value: 3
  },
  {
    label: 3.5,
    value: 3.5
  },
  {
    label: 4,
    value: 4
  },
];
Component({
  properties: {
    percentage: {
      type: Number,
      value: 0,
    },
    time: {
      type: Number,
      value: 0
    }
  },

  data: {
    // 组件内部数据
    closeFnType: false,
    currentIndex: 0,
    currentHeaderIndex: 0,
    ywjbPlaceholder: '7-13',
    ywtbPlaceholder: '4-10',
    ywgdcPlaceholder: '0',
    cwjbPlaceholder: '7-13',
    cwtbPlaceholder: '4-10',
    cwgdcPlaceholder: '0',
    ywjbValue: null,
    ywtbValue: null,
    ywgdcValue: 4,
    cwjbValue: null,
    cwtbValue: null,
    cwgdcValue: 4,
    numList: [],
    ywkgType: true,
    cwkgType: true,
    weightNum:null,
    list: [{
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_soft_unclicked.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_soft_clicked.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '较软',
        index: 0,
      },
      {
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_medium_unclicked.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_medium_clicked.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '中等',
        index: 1,
      },
      {
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_hard_unclicked.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_hard_clicked.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '较硬',
        index: 2,
      },
    ],
    listHeader: [{
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_cervical_spine_straighten_unselected.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_cervical_spine_straighten_selected.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '较软',
        index: 0,
      },
      {
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_normal_cervical_spine_unselected.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_normal_cervical_spine_selected.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '中等',
        index: 1,
      },
      {
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_normal_cervical_spine_unselected_cervical_spine_reverse_bow_unselected.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_cervical_spine_reverse_bow_selected.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '较硬',
        index: 2,
      },
    ],
  },

  lifetimes: {
    attached: function () {
      let _this = this;
      wx.getStorage({
        key: 'commforYwList',
        success (res) {
          if(res.data){
            _this.setData({
              ywjbValue:res.data.ywjbValue,
              ywtbValue:res.data.ywtbValue,
              ywgdcValue:res.data.ywgdcValue,
            })
          }
        }
      })
      wx.getStorage({
        key: 'commforCwList',
        success (res) {
          _this.setData({
            cwjbValue:res.data.cwjbValue,
            cwtbValue:res.data.cwtbValue,
            cwgdcValue:res.data.cwgdcValue,
          })
        }
      })
    },
    ready: function () {
      // 在组件在视图层布局完成后执行
    }
  },

  methods: {
    handleWeight(e) {
      let {
        type
      } = e.currentTarget.dataset;
      const {
        value
      } = e.detail;
      // if (type == 'jjjNum') {
      // 1. 过滤非法字符（只保留数字和第一个小数点）
      let values = value.replaceAll(/[^\d.]/g, '') // 去除非数字和小数点[2,4](@ref)
      // 2. 限制小数点后一位
      const reg = /^(\d+)?$/; // 允许整数或一位小数[1,2](@ref)
      if (!reg.test(value)) {
        values = value.substring(0, value.length - 1); // 删除最后一位非法字符
      }
      // 3. 更新数据（避免直接setData导致光标跳转）
      let {
        currentList
      } = this.data;
      currentList[type] = values;
      this.setData({
        currentList,
      });
      // }
    },
    checkRange(e) {
      let {
        type
      } = e.currentTarget.dataset;
      if (type=='weightNum'){
        let value = parseFloat(e.detail.value);
        if (isNaN(value) || value < 35 || value > 100) {
          wx.showToast({
            title: '请输入35-100之间的值',
            icon: 'none'
          });
          this.setData({
            weightNum: ''
          }); // 清空非法输入
        }else{
          this.setData({
            weightNum: value,
          });
        }
      }
    },
    pickerShow(e) {
      let {
        type
      } = e.currentTarget.dataset;
      if (type == 'ywjb') {
        this.setData({
          visible: true,
          numList: numList1,
          currentType: type,
        })
      } else if (type == 'ywtb') {
        this.setData({
          visible: true,
          numList: numList2,
          currentType: type,
        })
      } else if (type == 'ywgdc') {
        this.setData({
          visible: true,
          numList: numList3,
          currentType: type,
        })
      } else if (type == 'cwjb') {
        this.setData({
          visible: true,
          numList: numList1,
          currentType: type,
        })
      } else if (type == 'cwtb') {
        this.setData({
          visible: true,
          numList: numList2,
          currentType: type,
        })
      } else if (type == 'cwgdc') {
        this.setData({
          visible: true,
          numList: numList3,
          currentType: type,
        })
      }
    },
    onAlertChange(e) {
      let {
        currentType,
        ywkgType,
        cwkgType
      } = this.data;
      if (currentType == 'ywjb') {
        if (ywkgType) {
          let ywtb = this.data.ywgdcValue ? e.detail.value[0] - this.data.ywgdcValue : 0;
          let ywsxType = false;
          if (ywtb > 10) {
            ywtb = 10;
            ywsxType = true;
          } else if (ywtb < 0) {
            ywtb = 0
          }
          this.setData({
            ywtbValue: ywtb,
            ywsxType,
          })
        }
        if (e.detail.value[0] > 10) {
          wx.showToast({
            title: '建议调低高度，大多数人高于10cm有损颈椎健康',
            icon: 'none'
          })
        }
        this.setData({
          ywjbValue: e.detail.value[0],
        })
      } else if (currentType == 'ywtb') {
        if (ywkgType) {
          let ywjb = this.data.ywgdcValue ? this.data.ywgdcValue + e.detail.value[0] : 0;
          let ywsxType = false;
          if (ywjb > 13) {
            ywjb = 13;
            ywsxType = true;
          } else if (ywjb < 0) {
            ywjb = 0
          }
          this.setData({
            ywjbValue: ywjb,
            ywsxType,
          })
        }
        this.setData({
          ywtbValue: e.detail.value[0],
        })
      } else if (currentType == 'ywgdc') {
        this.setData({
          ywgdcValue: e.detail.value[0],
        })
      } else if (currentType == 'cwjb') {
        if (cwkgType) {
          let cwtb = this.data.cwgdcValue ? e.detail.value[0] - this.data.cwgdcValue : 0;
          let cwsxType = false;
          if (cwtb > 10) {
            cwtb = 10;
            cwsxType = true;
          } else if (cwtb < 0) {
            cwtb = 0
          }
          this.setData({
            cwtbValue: cwtb,
            cwsxType,
          })
        }
        if (e.detail.value[0] > 12) {
          wx.showToast({
            title: '建议调低高度，大多数人高于12cm有损颈椎健康',
            icon: 'none'
          })
        }
        this.setData({
          cwjbValue: e.detail.value[0],
        })
      } else if (currentType == 'cwtb') {
        if (cwkgType) {
          let cwjb = this.data.cwgdcValue ? this.data.cwgdcValue + e.detail.value[0] : 0;
          let cwsxType = false;
          if (cwjb > 13) {
            cwjb = 13;
            cwsxType = true;
          } else if (cwjb < 0) {
            cwjb = 0
          }
          this.setData({
            cwjbValue: cwjb,
            cwsxType,
          })
        }
        this.setData({
          cwtbValue: e.detail.value[0],
        })
      } else if (currentType == 'cwgdc') {
        this.setData({
          cwgdcValue: e.detail.value[0],
        })
      }
    },
    ywHandleChange(e) {
      this.setData({
        ywkgType: e.detail.value
      })
    },
    cwHandleChange(e) {
      this.setData({
        cwkgType: e.detail.value
      })
    },
    comforYwFn() {
      let {
        ywjbValue,
        ywtbValue,
        ywgdcValue,
        weightNum
      } = this.data;
      if (!ywjbValue) {
        wx.showToast({
          title: '请输入仰卧颈部的高度',
          icon: 'none'
        })
      } else if (!ywtbValue) {
        wx.showToast({
          title: '请输入仰卧颈部的高度',
          icon: 'none'
        })
      }else if(!weightNum){
        wx.showToast({
          title: '请输入体重',
          icon: 'none'
        })
        return
      }
      let commforYwList = {
        ywjbValue,
        ywtbValue,
        ywgdcValue
      }
      //把数据存到缓存里面
      wx.setStorage({
        key:"commforYwList",
        data:commforYwList
      });
      let setCon = `SET:07;${ywtbValue*10};${ywjbValue*10};${weightNum};00;@`
      this.triggerEvent('comforYwFn',setCon);
    },
    comforCwFn() {
      let {
        cwjbValue,
        cwtbValue,
        cwgdcValue,
        weightNum
      } = this.data;
      if (!cwjbValue) {
        wx.showToast({
          title: '请输入侧卧颈部的高度',
          icon: 'none'
        })
      } else if (!cwtbValue) {
        wx.showToast({
          title: '请输入侧卧颈部的高度',
          icon: 'none'
        })
      }else if(!weightNum){
        wx.showToast({
          title: '请输入体重',
          icon: 'none'
        })
        return
      }
      let commforCwList = {
        cwjbValue,
        cwtbValue,
        cwgdcValue
      }
      //把数据存到缓存里面
      wx.setStorage({
        key:"commforCwList",
        data:commforCwList
      });
      let setCon = `SET:08;${cwtbValue*10};${cwjbValue*10};${weightNum};00;@`
      this.triggerEvent('comforCwFn',setCon);
    },
    closeFn() {
      this.setData({
        closeFnType: true,
      })
      this.triggerEvent('closeFn')
    },
    selectFn(item) {
      let index = item.currentTarget.dataset.item;
      this.setData({
        currentIndex: index,
      })
    },
    selectHeaderFn(item) {
      let index = item.currentTarget.dataset.item;
      this.setData({
        currentHeaderIndex: index,
      })
    },
  }
})